#include "Pascal.h"

template <typename T>
procedure TFPGList<T>::Create()
{
	if (vList->size() > 0)
	{
		vList->clear();
	}
	vCount = 0;
	Last = NULL;
};

template <typename T>
procedure TFPGList<T>::Destroy()
{
	if (vList->size() > 0)
	{
		vList->clear();
	}
	vCount = 0;
	delete vList;
};

template <typename T>
procedure TFPGList<T>::Clear()
{
	if (vList->size() > 0)
	{
		vList->clear();
	}
	vCount = 0;
	Last = NULL;
};

template <typename T>
TFPGList<T>::TFPGList()
{
	vList = new std::list<T>();
	//vList->resize(0);
	vCount = 0;
	Last = NULL;
};

template <typename T>
Integer TFPGList<T>::Add(T vItem)
{
	vList->push_back(vItem);
	Last = &vItem;
	vCount = vList->size();
	return vCount - 1;
};

template <typename T>
Integer TFPGList<T>::Remove(const T Item)
{
	Integer vIndex;
	std::list<T>::iterator findIter = std::find(vList->begin(), vList->end(), Item);
	vIndex = std::distance(vList->begin(), findIter) + 1;
	vList->remove(Item);
	return vIndex;
};

template <typename T>
T TFPGList<T>::GetItem(Integer vIndex)
{
	auto it = vList->begin();
	std::advance(it, vIndex);
	return *it;
};

template <typename T>
T* TFPGList<T>::GetLast()
{
	return Last;
};

template <typename T>
Boolean TFPGList<T>::IsLast()
{
	return Last != NULL;
};

template <typename TKey, typename TData>
TKey TFPGMap<TKey,TData>::GetKey(Integer vIndex)
{
	auto it = vMap.begin();
	std::advance(it, vIndex);
	return it->first;
};

template <typename TKey, typename TData>
procedure TFPGMap<TKey,TData>::PutKey(TKey vKey, Integer vIndex)
{
	auto it = vMap.begin();
	std::advance(it, vIndex);
	(TKey)it->first = vKey;
};

template <typename TKey, typename TData>
procedure TFPGMap<TKey,TData>::Create()
{
	vMap.clear();
	Count = vMap.size();
};

template <typename TKey, typename TData>
procedure TFPGMap<TKey,TData>::Destroy()
{
	vMap.clear();
	Count = vMap.size();
	vMap.~map();
};

template <typename TKey, typename TData>
procedure TFPGMap<TKey,TData>::Remove(TKey vKey)
{
    auto vIt = vMap.begin();
    auto vEnd = vMap.end();
	while (vIt != vEnd)
	{
		if ((TKey)vIt->first == vKey)
		{
			vMap.erase(vIt);
			break;
		}
		vIt++;
	}
};
/*
void DebugLn(PChar vStr)
{
	//
}

void DebugLnEnter(PChar vStr)
{
	//
}

void DebugLnExit()
{
	//
}*/
